﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrenzDenWebSite.Business;
using System.Web.UI.HtmlControls;

namespace FrenzDenWebSite
{
    public partial class AccountSettingForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies[SiteConstants.COOKIESTRINGPROFILE] == null)
            {
                Response.Redirect("~/Default.aspx?prev=Home&showmsg=1");
            }

            if (!IsPostBack)
            {
                LoadYear();
                GetProfileInformation();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            try
            {
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    long profileId = SiteCommon.DecryptSession(Request.Cookies[SiteConstants.COOKIESTRINGPROFILE].Value).ProfileID;
                    dataContext.UpdateProfileInfo(profileId, txtCurrentCity.Text, Convert.ToInt16(ddlCurrentCityPrivacy.SelectedValue),
                                                    txtHomeCity.Text, Convert.ToInt16(ddlHomeCityPrivacy.SelectedValue), ddlGender.SelectedValue,
                                                    Convert.ToDateTime(string.Format("{0}-{1}-{2}", ddlYear.SelectedValue, ddlMonth.SelectedValue, ddlDate.SelectedValue)),
                                                    Convert.ToInt16(ddlBirthdayPrivacy.SelectedValue), txtInterestedIn.Text, Convert.ToInt16(ddlInterestedInPrivacy.SelectedValue),
                                                    txtLanguages.Text, Convert.ToInt16(ddlLanguagesPrivacy.SelectedValue), txtAboutMe.Text,
                                                    Convert.ToInt16(ddlAboutMePrivacy.SelectedValue));
                    GetProfileInformation();
                    msgbox = new MessageBox("Successfully updated!");
                    (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
                }
            }
            catch
            {
                msgbox = new MessageBox(SiteConstants.ERRORMSG);
                (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
            }
        }

        private void LoadYear()
        {
            int endYear = DateTime.Now.Year - 10;
            int startYear = endYear - 80;

            for (int i = endYear; i >= startYear; i--)
            {
                ddlYear.Items.Add(i.ToString());
            }
        }

        private void GetProfileInformation()
        {
            try
            {
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    long profileId = SiteCommon.DecryptSession(Request.Cookies[SiteConstants.COOKIESTRINGPROFILE].Value).ProfileID;
                    var result = dataContext.GetProfileInfo(profileId);
                    foreach (var item in result)
                    {
                        txtCurrentCity.Text = item.CurrentCity;
                        ddlCurrentCityPrivacy.SelectedValue = item.CurrentCityPrivacy.ToString();
                        txtHomeCity.Text = item.HomeCity;
                        ddlHomeCityPrivacy.SelectedValue = item.HomeCityPrivacy.ToString();
                        ddlGender.SelectedValue = item.Gender;
                        txtInterestedIn.Text = item.InterestedIn;
                        ddlInterestedInPrivacy.SelectedValue = item.InterestedInPrivacy.ToString();
                        txtLanguages.Text = item.Languages;
                        ddlLanguagesPrivacy.SelectedValue = item.LanguagesPrivacy.ToString();
                        txtAboutMe.Text = item.AboutMe;
                        ddlAboutMePrivacy.SelectedValue = item.AboutMePrivacy.ToString();
                        ddlBirthdayPrivacy.SelectedValue = item.BirthdayPrivacy.ToString();
                        DateTime birthDay = item.BirthDay;
                        ddlDate.SelectedValue = birthDay.Day.ToString();
                        ddlMonth.SelectedValue = birthDay.Month.ToString();
                        ddlYear.SelectedValue = birthDay.Year.ToString();
                    }

                }
            }
            catch
            {
                MessageBox msgbox = new MessageBox("Error getting profile information. Please try again later!");
                (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
            }
        }
    }
}